package com.tzxs.pay.main.deposit.pop;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tzxs.pay.R;
import com.tzxs.pay.main.deposit.TypeNavigator;

public class PayTypeFragment extends Fragment implements View.OnClickListener {
    private static PayTypeFragment payTypeFragment;

    private TextView aliPay;
    private TextView wx;
    private TextView bank;
    private TextView all;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        return inflater.inflate(R.layout.pop_pay_type, container, false);

    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        setNavigator((TypeNavigator) context);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        aliPay = view.findViewById(R.id.pay_type_alipay);
        wx = view.findViewById(R.id.pay_type_wx);
        bank = view.findViewById(R.id.pay_type_bank);
        all = view.findViewById(R.id.pay_type_all);
        all.setSelected(true);
        aliPay.setOnClickListener(this);
        wx.setOnClickListener(this);
        bank.setOnClickListener(this);
        all.setOnClickListener(this);
    }

    /**
     * 获取单例
     *
     * @return payTypeFragment
     */
    public static PayTypeFragment getInstance() {
        if (payTypeFragment == null) {
            payTypeFragment = new PayTypeFragment();

        }
        return payTypeFragment;
    }

    /**
     * 清除选择
     */
    private void clearChoose() {
        aliPay.setSelected(false);
        wx.setSelected(false);
        bank.setSelected(false);
        all.setSelected(false);
    }

    @Override
    public void onClick(View v) {
        clearChoose();
        switch (v.getId()) {
            //支付宝
            case R.id.pay_type_alipay:
                navigator.getPayTypeStr("支付宝");
                aliPay.setSelected(!aliPay.isSelected());
                break;
            //微信
            case R.id.pay_type_wx:
                navigator.getPayTypeStr("微信");
                wx.setSelected(!wx.isSelected());

                break;
            //银联
            case R.id.pay_type_bank:
                navigator.getPayTypeStr("银联");
                bank.setSelected(!bank.isSelected());
                break;
            //不限
            case R.id.pay_type_all:
                navigator.getPayTypeStr("不限");
                all.setSelected(!all.isSelected());
                break;
        }
    }

    private TypeNavigator navigator;

    public void setNavigator(TypeNavigator navigator) {
        this.navigator = navigator;
    }
}
